package com.jz;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Date;

/**
 * Created by Hasee on 2022-03-08.
 */
@RestController
public class TestController {
    @Autowired
    private Config config;

    @Autowired
    private FtpUtil ftpUtil;


    @PostMapping("upload")
    public String upload(MultipartFile file)throws Exception{
//        if ()
        System.out.println(config.getHost());
//        FtpUtil ftpUtil = new FtpUtil();
        //压缩 jdk自带压缩算法，对图片可以进行任意压缩
        File file2 = yasuoImg(file.getInputStream());
        FileInputStream fileInputStream = new FileInputStream(file2);
        boolean b = ftpUtil.uploadFile(fileInputStream);
        if (!b){
            return "上传失败";
        }
        return "SUCCESS";
    }

    public static File yasuoImg(InputStream input) throws Exception{
        System.out.println("开始：" + new Date().toLocaleString());
        YaSuoUtil imgCom = new YaSuoUtil(input);
        File path = imgCom.resizeFix(1000, 1000);
        System.out.println("结束：" + new Date().toLocaleString());
        return path;
    }
}
